package torres.item;

import java.util.List;

import torres.action.Action;
import torres.entity.Entity;
import torres.fileIO.BasicContainer;
import torres.fileIO.ClassContainer;
import torres.entity.Sneak;
import torres.utility.Statistics;

/**
 * @author Torres
 * A special kind of item that can be disabled by a Sneak.
 */

public class TrapItem extends Item {

	private List<Action> actions;
	private int difficulty;
	private boolean visible;
	private boolean detectAttempted;
	/**
	 * Creates a new TrapItem with the specified name and Interactions.
	 * @param name The name of the TrapItem.
	 * @param intList The Interactions to be performed when this trap is sprung.
	 */
	public TrapItem(String name, int id, List<Action> actions) {
		super(name, id);
		this.setActions(actions);
		this.difficulty = 1;
		this.setInvisible();
		this.detectAttempted = false;
	}
	
	/**
	 * Creates a new TrapItem which is a copy of the specified TrapItem.
	 * @param item The TrapItem to be copied.
	 */
	public TrapItem(TrapItem item) {
		this(item.getName(), item.getID(), item.getActions());
	}
	
	/**
	 * Triggers the effect of an Entity walking on this item.
	 * @param e The Entity that is walking on this item
	 */
	public void onTouch(Entity e) {
		this.setVisible();
		if(this.getActions() != null) {
				for(Action a : this.getActions()) { 
					a.setUp(e);
					Thread t = new Thread(a);
					t.setName("TrapItem" + t.getId());
					t.start();
				}
		}
	}
	
	/**
	 * Set the actions of this item to the specified list.
	 * @param actions The new list of Actions.
	 */
	public void setActions(List<Action> actions) {
		this.actions = actions;
	}
	
	/**
	 * Get the list of Actions that occur on use.
	 * @return The list of Actions that occur on use.
	 */
	public List<Action> getActions() {
		return this.actions;
	}

	/**
	 * A method that attempts to remove the TrapItem from map.
	 * @param detectRemoveTrapSkill
	 */
	public void attemptToRemove(int detectRemoveTrapSkill) {
		if(Statistics.attempt(difficulty, detectRemoveTrapSkill)) {
			this.setConsumed();
		}
	}
	
	public boolean isVisible() {
		return this.visible;
	}
	
	public void setVisible() {
		this.visible = true;
	}
	
	public void setInvisible() {
		this.visible = false;
	}
	
	public void attemptDetect(Entity e) {
		if(!detectAttempted) {
			System.out.println("TrapItem: Trying to find the trap!");
			Sneak s = (Sneak)e.getOccupation();
			if(Statistics.attempt(difficulty, s.getDetectRemoveTrapSkill())) {
				System.out.println("We found the trap!");
				this.setVisible();
			}
			detectAttempted = true;
		}
	}
	
	@Override
	public ClassContainer save() {
		ClassContainer curClass = super.save();
		curClass.addProperty(new BasicContainer("type", "TrapItem"));
		
		return curClass;
		
	}
}
